package com.caigou.modules.personal.ui.page.fragment

import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.caigou.comm.base.bean.AppBackgroundInfo
import com.caigou.comm.base.bus.BusKey
import com.caigou.comm.base.bus.LiveDataBus
import com.caigou.comm.base.constants.AppConstants
import com.caigou.comm.base.constants.RouterPath
import com.caigou.comm.base.container.BaseFragment
import com.caigou.comm.base.ext.Resource
import com.caigou.comm.base.ext.observe
import com.caigou.comm.base.ext.showToast
import com.caigou.comm.base.widget.SpaceItemDecoration
import com.caigou.modules.personal.adapter.FamousItemAdapter
import com.caigou.modules.personal.bean.FamousItemBean
import com.caigou.modules.personal.databinding.FragmentPageFamousBinding
import com.caigou.modules.personal.ui.page.PersonalPageActivity
import com.caigou.modules.personal.viewmodel.PersonalPageViewModel
import dagger.hilt.android.AndroidEntryPoint

/**
 * @author #Suyghur.
 * Created on 2022/01/04
 */
@AndroidEntryPoint
class MacaoFamousFragment : BaseFragment() {
    private var _binding: FragmentPageFamousBinding? = null
    private val binding get() = _binding!!

    private val personalPageViewModel: PersonalPageViewModel by viewModels()

    private val mAdapter: FamousItemAdapter by lazy {
        FamousItemAdapter(mutableListOf())
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        _binding = FragmentPageFamousBinding.inflate(inflater, container, false)
        return _binding?.root
    }

    override fun initView() {
        binding.pageFamousRecyclerView.run {
            layoutManager = LinearLayoutManager(requireActivity())
            adapter = mAdapter
            itemAnimator = DefaultItemAnimator()
            addItemDecoration(SpaceItemDecoration(requireActivity()))
        }

        mAdapter.setOnItemClickListener { _, _, position ->
            ARouter.getInstance().build(RouterPath.Famous.PAGE_PERSONAL)
                .withInt(AppConstants.FAMOUS_TYPE, 2)
                .withString(AppConstants.USER_ID, AppBackgroundInfo.instance.userBean!!.userId)
                .navigation()
        }
    }

    override fun lazyLoad() {
        fetchInfo()
    }

    override fun onBack() {
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    override fun observeViewModel() {
        super.observeViewModel()
        observe(personalPageViewModel.famousListLiveData, ::handleGetFamousList)
    }

    private fun handleGetFamousList(result: Resource<MutableList<FamousItemBean>>) {
        when (result) {
            is Resource.Loading -> {
                mDialog.show()
            }
            is Resource.DataError -> {
                mDialog.dismiss()
                if (!TextUtils.isEmpty(result.errorMsg)) {
                    showToast(result.errorMsg)
                } else {
                    showToast("获取个人主页信息异常")
                }
            }
            is Resource.Success -> {
                mDialog.dismiss()
                if (result.data == null) {
                    binding.pageFamousLlEmpty.visibility = View.VISIBLE
                    binding.pageFamousRecyclerView.visibility = View.GONE
                    return
                }

                if (result.data!!.isNullOrEmpty()) {
                    binding.pageFamousLlEmpty.visibility = View.VISIBLE
                    binding.pageFamousRecyclerView.visibility = View.GONE
                } else {
                    binding.pageFamousLlEmpty.visibility = View.GONE
                    binding.pageFamousRecyclerView.visibility = View.VISIBLE
                }


                mAdapter.setList(result.data!!)

                LiveDataBus.get().with(BusKey.LOAD_VP2_FINISH).value = Any()
            }
        }
    }

    private fun fetchInfo() {
        val userId = (requireActivity() as PersonalPageActivity).userId
        personalPageViewModel.getFamousList(2, userId)
    }
}